package register;

import com.xiguanlezz.cn.client.naming.core.ServerListManager;
import com.xiguanlezz.cn.client.env.NacosClientProperties;
import com.xiguanlezz.cn.client.naming.remote.NamingClientProxyDelegate;
import com.xiguanlezz.cn.client.naming.remote.grpc.NamingGrpcClientProxy;
import com.xiguanlezz.cn.client.naming.remote.http.NamingHttpClientProxy;
import com.xiguanlezz.cn.api.naming.pojo.Instance;
import org.junit.Assert;
import org.junit.Test;

import java.util.Properties;

/**
 * @Author：习惯了种种
 * @CreateDate：2024/11/18 16:33
 * @Description：
 */
public class _1_总体框架搭建 {
    String namespace = "hangzhou";

    @Test
    public void simpleServerListManager() {
        Properties properties = new Properties();
        properties.setProperty("serverAddr", "127.0.0.1:8848");
        NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
        ServerListManager serverListManager = new ServerListManager(namespace, nacosClientProperties);
        String currentServer = serverListManager.getCurrentServer();
        Assert.assertEquals("127.0.0.1:8848", currentServer);
        currentServer = serverListManager.genNextServer();
        Assert.assertEquals("127.0.0.1:8848", currentServer);
    }

    @Test
    public void clientProxyDelegate() {
        Properties properties = new Properties();
        properties.setProperty("serverAddr", "127.0.0.1:8848");
        NacosClientProperties nacosClientProperties = NacosClientProperties.PROTOTYPE.derive(properties);
        NamingClientProxyDelegate clientProxyDelegate = new NamingClientProxyDelegate(namespace, nacosClientProperties);
        Instance ephemeralInstance = new Instance();
        Assert.assertEquals(
                NamingGrpcClientProxy.class,
                clientProxyDelegate.getExecuteClientProxy(ephemeralInstance).getClass()
        );
        Instance noEphemeralInstance = new Instance();
        noEphemeralInstance.setEphemeral(false);
        Assert.assertEquals(
                NamingHttpClientProxy.class,
                clientProxyDelegate.getExecuteClientProxy(noEphemeralInstance).getClass()
        );
    }

}
